<?php

namespace app\queue;

use Webman\RedisQueue\Client;
use Webman\RedisQueue\Consumer;
use Webman\RedisQueue\Redis;

abstract class Queue implements Consumer
{

    //连接名，对应 plugin/webman/redis-queue/redis.php 里的连接
    public $connection = 'default';

    public $queue;

    public function __construct(){
        if(!$this->queue){
            $this->queue=unCamelCase(static::class);
        }
    }

    public abstract function consume($data);

    //投递
    public function send($data, $delay = 0)
    {
        if(!$this->queue){
            throw new \Exception('queue name invalid',500);
        }

        //同步
        return Redis::connection($this->connection)->send($this->queue, $data, $delay);
    }

    static function dispatch($data, $delay = 0)
    {
        return (new static())->send($data,$delay);
    }
}